//
//  RequestManager.m
//  片刻
//
//  Created by xalo on 12/7/8.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "RequestManager.h"
#import <AFNetworking.h>
@implementation RequestManager


+ (void)fetchDataWithUrl:(NSString *)url parameter:(NSDictionary *)dic method:(NSString *)method compeleted:(RequestCompeleted)compeleted updateUI:(UpdateUI)update{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        if ([method isEqualToString:@"GET"]) {
            [manager GET:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                compeleted((NSData *)responseObject);
                dispatch_async(dispatch_get_main_queue(), ^{
                    update();
                });
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        } else {
            [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                //把Data传出去
                compeleted((NSData *)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    update();
                });
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        }
    });
}


@end
